package com.qfedu.defect.reception.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qfedu.defect.reception.dto.DefectReceptionDto;
import com.qfedu.defect.reception.mapper.DefectReceptionMapper;
import com.qfedu.defect.reception.pojo.DefectReception;
import com.qfedu.defect.reception.service.DefectReceptionService;
import com.qfedu.defect.task.dto.DefectTaskDto;
import com.qfedu.defect.task.mapper.DefectTaskMapper;
import com.qfedu.defect.task.pojo.DefectTask;
import com.qfedu.defect.task.service.DefectTaskService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
public class DefectReceptionServiceImpl extends ServiceImpl<DefectReceptionMapper, DefectReception> implements DefectReceptionService{




    @Transactional(readOnly = true)
    @Override
    public Page<DefectReception> getDefectReceptions(DefectReceptionDto receptionDto) {
        LambdaQueryWrapper<DefectReception> wrapper = new LambdaQueryWrapper<>();
        String finishInfo = receptionDto.getFinishInfo();
        wrapper.like(StringUtils.hasLength(finishInfo), DefectReception::getFinishInfo, finishInfo);
        Integer state = receptionDto.getState();
        wrapper.eq(state != null, DefectReception::getState, state);
        String operationUser = receptionDto.getOperationUser();
        wrapper.eq(StringUtils.hasLength(operationUser), DefectReception::getOperationUser, operationUser);

        Page<DefectReception> page = new Page<>(receptionDto.getCurrentPage(), receptionDto.getPageSize());
        return page(page, wrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean save(DefectReception defectReception) {
        String operationUser = defectReception.getOperationUser();
        defectReception.setState(StringUtils.hasLength(operationUser) ? 1 : 0);
        return super.save(defectReception);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateById(DefectReception defectReception) {
//        String operationUser = defectReception.getOperationUser();
//        defectReception.setState(StringUtils.hasLength(operationUser) ? 1 : 0);
        return super.updateById(defectReception);
    }

}
